<?php

namespace App\Http\Controllers;

use App\MakeTable\Table;
use App\Mentor;
use App\Redict\Success;
use App\SoftImage\SoftImage;
use Illuminate\Http\Request;
use Gate;

class MentorController extends Controller
{
    //
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $pagename = '教练列表';
        $modelurl = 'mentors';
        $tablecate = ['tablecate'=>'教练'];
        $headname = ['名称','照片','联系地址','简介','旗下课程','所属俱乐部','操作'];
        $tablehead = ['name','image','contact_address','intro','clubclasses','club','id'];
        $mentortable = new Table($tablehead,$page = 8);
        if(Gate::allows('club_Admin_mentors')){
            $model = $mentortable->viewdatawithuser(Mentor::class);
            $models = $mentortable->viewpagedatawithuser(Mentor::class);
        }
        else{
            $model = $mentortable->viewdata(Mentor::class);
            $models = $mentortable->viewpagedata(Mentor::class);
        }

        return view('admin.home.tableshow',compact('tablecate','model','tablehead','models','headname','pagename','modelurl'));
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if (Gate::denies('club_Admin_mentors')) {
            return redirect('admin/mentors');
        }
        //新增培训
        $modelurl = 'mentors';
        $pagename = '新增教练';
        $formhead = ['name','contact_address','image','intro'];
        $formname = ['名字','联系地址','头像照片','简介',];
        $formhead = collect($formname)->zip($formhead)->toArray();//压缩成数组

        return view('admin.home.mentorform',compact('pagename','modelurl','formhead'));
        //表单，位置 resources/views/admin/home/activityform.blade
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //添加验证
        $data = array_merge(
            ['club_id'=>\Auth::user()->clubroleid(),
            ],
            $request->all(),
            SoftImage::img($request->file('image'))//处理图片的函数
        );

        $mentor = Mentor::create($data);
//        $soft->tags()->attach($request->input('tags_list')); duoduiduo

        return redirect('admin/mentors')->with('message', '成功添加'.$mentor->name);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        if (Gate::denies('club_Admin_mentors')) {
            return redirect('admin/mentors');
        }
        $mentor = Mentor::findOrFail($id);
        $modelurl = 'mentors';
        $pagename = $mentor->name;
        $formhead = ['name','contact_address','image','intro'];
        $formname = ['名字','联系地址','头像照片','简介',];
        $formhead = collect($formname)->zip($formhead)->toArray();
        return view('admin.home.editmentor',compact('pagename','mentor','formhead','modelurl'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        //添加验证
        $mentor = Mentor::findOrFail($id);
        //查找
        $data = array_merge(
            $request->all(),
            ['mentor_id'=>$request->input('mentor')]
        );
        if($request->file('image')){

            $data = array_merge($data, SoftImage::img($request->file('image')));
        }

        $mentor->update($data);
        //更新
        return Success::back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除
        Mentor::destroy($id);
        return Success::back();
    }
}
